package myicesscheduler.web;

import static org.junit.Assert.*;

import java.util.Calendar;
import java.util.Date;

import myicesscheduler.hibernate.ReservationTypeTransaction;

import org.junit.Test;

public class LectureCommandTest
{
   final int whatTimeWillBeStartOfDay = 8;
   final int whatTimeWillBeEndOfDay = 21;
   final int howMuchHourIsWorkingHour = whatTimeWillBeEndOfDay - whatTimeWillBeStartOfDay;

   @Test
   public void SetTimeSlotCommands()
   {
      final int meetingTypeId = ReservationTypeTransaction.getTypeId_meetingWithIndividual();
      final LectureCommand cmd = new LectureCommand();
      cmd.setTimeSlotIntervalInMinute( 30 );

      assertEquals( howMuchHourIsWorkingHour * 2, cmd.getTimeSlotCommands().length );
      for( int i = 0; i < howMuchHourIsWorkingHour; ++i )
      {
         assertEquals( whatTimeWillBeStartOfDay + i, cmd.getTimeSlotCommands()[ i * 2 ]
               .getBeginHour() );
         assertEquals( whatTimeWillBeStartOfDay + i, cmd.getTimeSlotCommands()[ i * 2 + 1 ]
               .getBeginHour() );

         assertEquals( 0, cmd.getTimeSlotCommands()[ i * 2 ].getBeginMin() );
         assertEquals( 30, cmd.getTimeSlotCommands()[ i * 2 + 1 ].getBeginMin() );

         assertEquals( meetingTypeId, cmd.getTimeSlotCommands()[ i * 2 ].getReservationType() );
         assertEquals( meetingTypeId, cmd.getTimeSlotCommands()[ i * 2 + 1 ].getReservationType() );
      }
   }

   @Test
   public void MergeConsequtiveTimeSlots()
   {
      final int lectureType = ReservationTypeTransaction.getTypeId_lecture();
      final int notAvailableType = ReservationTypeTransaction.getTypeId_notAvailable();

      final LectureCommand cmd = new LectureCommand();
      cmd.setTimeSlotIntervalInMinute( 60 );
      assertEquals( howMuchHourIsWorkingHour, cmd.getTimeSlotCommands().length );
      for( int i = 0; i < howMuchHourIsWorkingHour; ++i )
         cmd.getTimeSlotCommands()[ i ].setEnabled( true );

      cmd.getTimeSlotCommands()[ 0 ].setReservationType( notAvailableType );
      cmd.getTimeSlotCommands()[ 1 ].setReservationType( lectureType );
      cmd.getTimeSlotCommands()[ 2 ].setReservationType( lectureType );
      cmd.getTimeSlotCommands()[ 3 ].setReservationType( notAvailableType );
      cmd.mergeConsequtiveTimeSlots();

      assertEquals( howMuchHourIsWorkingHour - 1, cmd.getTimeSlotCommands().length );
      assertEquals( notAvailableType, cmd.getTimeSlotCommands()[ 0 ].getReservationType() );
      assertEquals( lectureType, cmd.getTimeSlotCommands()[ 1 ].getReservationType() );
      assertEquals( notAvailableType, cmd.getTimeSlotCommands()[ 2 ].getReservationType() );
      assertEquals( 120, cmd.getTimeSlotCommands()[ 1 ].getLengthInMin() );
   }

   @Test
   public void DefaultValues()
   {
      final Calendar today = Calendar.getInstance();
      today.setTime( new Date() );
      final int year = today.get( Calendar.YEAR );
      final int month = today.get( Calendar.MONTH );
      final int day = today.get( Calendar.DAY_OF_MONTH );

      final LectureCommand cmd = new LectureCommand();
      assertEquals( year, cmd.getDateYear() );
      assertEquals( LectureCommand.Month.getMonth( month ), cmd.getDateMonth() );
      assertEquals( day, cmd.getDateDay() );

      final int timeInterVal = 60;
      cmd.setTimeSlotIntervalInMinute( timeInterVal );
      final Calendar beginDate = Calendar.getInstance();
      beginDate.setTime( cmd.getTimeSlotCommands()[ 0 ].getBeginDate() );
      assertEquals( year, beginDate.get( Calendar.YEAR ) );
      assertEquals( month, beginDate.get( Calendar.MONTH ) );
      assertEquals( day, beginDate.get( Calendar.DAY_OF_MONTH ) );
      assertEquals( whatTimeWillBeStartOfDay, beginDate.get( Calendar.HOUR_OF_DAY ) );
      assertEquals( 0, beginDate.get( Calendar.MINUTE ) );

      final Calendar endDate = Calendar.getInstance();
      endDate.setTime( cmd.getTimeSlotCommands()[ 0 ].getEndDate() );
      assertEquals( year, endDate.get( Calendar.YEAR ) );
      assertEquals( month, endDate.get( Calendar.MONTH ) );
      assertEquals( day, endDate.get( Calendar.DAY_OF_MONTH ) );
      assertEquals( whatTimeWillBeStartOfDay + timeInterVal / 60, endDate
            .get( Calendar.HOUR_OF_DAY ) );
      assertEquals( 0, endDate.get( Calendar.MINUTE ) );
   }

   @Test
   public void setTimeSlotIntervalInMinute_enable()
   {
      final LectureCommand cmd = new LectureCommand();
      cmd.setTimeSlotIntervalInMinute( 60 );
      assertTrue( cmd.getTimeSlotCommands()[ 0 ].isEnabled() );
      assertTrue( cmd.getTimeSlotCommands()[ 1 ].isEnabled() );

      cmd.setTimeSlotIntervalInMinute( 30 );
      assertTrue( cmd.getTimeSlotCommands()[ 0 ].isEnabled() );
      assertTrue( cmd.getTimeSlotCommands()[ 1 ].isEnabled() );
   }
}
